package com.rc.evcharger.dto.terminal;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.terminal.RecordSource;
import com.rc.evcharger.common.terminal.Result;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.dto.msg.TerminalMessage;
import com.rc.evcharger.dto.utils.MessageUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 充电记录（返回来的数据格式）
 * @author kb
 */
public class ElecRecordRequest extends RecordSource implements Result {
    /**
     * 交易类型
     */
    @ByteLength(length = 1)
    private String tradeType;
    /**
     * 交易流水号
     */
    @ByteLength(length = 5)
    private String serialNum;
    /**
     * 地区代码
     */
    @ByteLength(length = 3)
    private String areaCode;
    /**
     * 开始卡号
     */
    @ByteLength(length = 8)
    private String beginCard;
    /**
     * 结束卡号
     */
    @ByteLength(length = 8)
    private String endCard;
    /**
     * 开始卡型
     */
    @ByteLength(length = 2)
    private String beginCardType;
    /**
     * 结束卡型
     */
    @ByteLength(length = 2)
    private String endCardType;
    /**
     * 开始交易电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal beginPower;
    /**
     * 开始交易费率1电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal beginRatePower1;
    /**
     * 开始交易费率2电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal beginRatePower2;
    /**
     * 开始交易费率3电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal beginRatePower3;
    /**
     * 开始交易费率4电量行度
     */
    @ByteLength(length = 4, decimal = 1)
    private BigDecimal beginRatePower4;
    /**
     * 结束交易电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal endPower;
    /**
     * 结束交易费率1电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal endRatePower1;
    /**
     * 结束交易费率2电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal endRatePower2;
    /**
     * 结束交易费率3电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal endRatePower3;
    /**
     * 结束交易费率4电量行度
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal endRatePower4;
    /**
     * 交易费率1电价
     */
    @ByteLength(length = 3, decimal = 4)
    private BigDecimal ratePrice1;
    /**
     * 交易费率2电价
     */
    @ByteLength(length = 3, decimal = 4)
    private BigDecimal ratePrice2;
    /**
     * 交易费率3电价
     */
    @ByteLength(length = 3, decimal = 4)
    private BigDecimal ratePrice3;
    /**
     * 交易费率4电价
     */
    @ByteLength(length = 3, decimal = 4)
    private BigDecimal ratePrice4;
    /**
     * 停车费单价
     */
    @ByteLength(length = 3, decimal = 2)
    private BigDecimal parkPrice;
    /**
     * 开始交易时间
     */
    @ByteLength(length = 6)
    private Date beginTime;
    /**
     * 结束交易时间
     */
    @ByteLength(length = 6)
    private Date endTime;
    /**
     * 停车费
     */
    @ByteLength(length = 3, decimal = 2)
    private BigDecimal parkMoney;
    /**
     * 燃气燃油加工费
     */
    @ByteLength(length = 3, source = SocketConstant.SOURCE_SHENZHEN)
    private BigDecimal processPrice;
    /**
     * 燃气燃油加工费
     */
    @ByteLength(length = 4, source = SocketConstant.SOURCE_SHENZHEN)
    private BigDecimal processFee;
    /**
     * 交易前余额
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal beforeBalance;
    /**
     * 交易后余额
     */
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal afterBalance;
    /**
     * 卡交易计数器
     */
    @ByteLength(length = 5)
    private String counter;
    /**
     * 终端号
     */
    @ByteLength(length = 5)
    private String rtua;
    /**
     * 卡版本号
     */
    @ByteLength(length = 5)
    private String cardVersion;
    /**
     * POS机号
     */
    @ByteLength(length = 5)
    private String posNum;
    /**
     * 卡状态
     */
    @ByteLength(length = 1)
    private String cardStatus;
    /**
     * 车辆识别码
     */
    @ByteLength(length = 17, source = SocketConstant.SOURCE_SHENZHEN)
    private String svin;
    /**
     * 车辆识别码
     */
    @ByteLength(length = 3, source = SocketConstant.SOURCE_ZHUHAI)
    private String zvin;
    /**
     * 枪号
     */
    @ByteLength(length = 1, source = SocketConstant.SOURCE_ZHUHAI)
    private String gunNum;

    /**
     * 服务费
     */
    @ByteLength(length = 4, decimal = 1, source = SocketConstant.SOURCE_ZHUHAI)
    private BigDecimal servicePrice;

    /**
     * 计算深圳和珠海的所有字段数据长度然后和dataLen比较来判断数据来自哪一种硬件
     * @param dataLen
     */
    public ElecRecordRequest(int dataLen) {
        Field[] fields = this.getClass().getDeclaredFields();
        int shenzhen = 0;
        int zhuhai = 0;
        for (int i = 0; i < fields.length; i++) {
            ByteLength bl = fields[i].getAnnotation(ByteLength.class);
            if (bl == null) {
                continue;
            }

            if (bl.source().equals(SocketConstant.SOURCE_ALL)) {
                shenzhen += bl.length();
                zhuhai += bl.length();
            } else if (bl.source().equals(SocketConstant.SOURCE_SHENZHEN)) {
                shenzhen += bl.length();
            } else {
                zhuhai += bl.length();
            }

        }

        if (shenzhen == dataLen) {
            this.length = shenzhen * 2 + shenzhen - 1;
            this.source = SocketConstant.SOURCE_SHENZHEN;
        } else {
            this.length = zhuhai * 2 + zhuhai - 1;
            this.source = SocketConstant.SOURCE_ZHUHAI;
        }
    }

    /**
     * 遍历所有字段，按顺序和长度截取数据
     * @param str    数据域
     * @param source 硬件产商
     */
    public ElecRecordRequest(String str, String source) {
        Field[] fields = this.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            ByteLength bl = fields[i].getAnnotation(ByteLength.class);
            if (bl == null || !(bl.source().equals(SocketConstant.SOURCE_ALL) || bl.source().equals(source))) {
                continue;
            }
            int len = bl.length() * 2 + bl.length() - 1;
            String value = str.substring(0, len);
            if (str.length() > len)
                str = str.substring(len + 1);
            setValue(fields[i], bl, value);
        }
    }

    /**
     * @param field
     * @param bl
     * @param value
     */
    private void setValue(Field field, ByteLength bl, String value) {
        try {
            if (field.getType().equals(BigDecimal.class)) {
                field.set(this, DataUtil.getBDValue(value, bl.decimal(), false));
            } else if (field.getType().equals(Date.class)) {
                StringBuffer sb = new StringBuffer();
                String[] vals = value.split(DataUtil.SPLIT);
                sb.append(vals[5]).append("-").append(vals[4]).append("-").append(vals[3]).append(DataUtil.SPLIT)
                        .append(vals[2]).append(":").append(vals[1]).append(":").append(vals[0]);
                String format = "yy-MM-dd HH:mm:ss";
                Date date = DateHelper.stringToDate(sb.toString(), format);
                String dateStr = DateHelper.dataToString(date, DateHelper.DATE_COMMON_FORMAT);
                date = DateHelper.stringToDate(dateStr, DateHelper.DATE_COMMON_FORMAT);
                //SimpleDateFormat sdf = new SimpleDateFormat("YYMMDD HHmmSS");
                field.set(this, date);
            } else {
                field.set(this, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getTradeType() {
        return tradeType;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public String getSerialNum() {
        return serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getBeginCard() {
        return beginCard;
    }

    public void setBeginCard(String beginCard) {
        this.beginCard = beginCard;
    }

    public String getEndCard() {
        return endCard;
    }

    public void setEndCard(String endCard) {
        this.endCard = endCard;
    }

    public String getBeginCardType() {
        return beginCardType;
    }

    public void setBeginCardType(String beginCardType) {
        this.beginCardType = beginCardType;
    }

    public String getEndCardType() {
        return endCardType;
    }

    public void setEndCardType(String endCardType) {
        this.endCardType = endCardType;
    }

    public BigDecimal getBeginPower() {
        return beginPower;
    }

    public void setBeginPower(BigDecimal beginPower) {
        this.beginPower = beginPower;
    }

    public BigDecimal getBeginRatePower1() {
        return beginRatePower1;
    }

    public void setBeginRatePower1(BigDecimal beginRatePower1) {
        this.beginRatePower1 = beginRatePower1;
    }

    public BigDecimal getBeginRatePower2() {
        return beginRatePower2;
    }

    public void setBeginRatePower2(BigDecimal beginRatePower2) {
        this.beginRatePower2 = beginRatePower2;
    }

    public BigDecimal getBeginRatePower3() {
        return beginRatePower3;
    }

    public void setBeginRatePower3(BigDecimal beginRatePower3) {
        this.beginRatePower3 = beginRatePower3;
    }

    public BigDecimal getBeginRatePower4() {
        return beginRatePower4;
    }

    public void setBeginRatePower4(BigDecimal beginRatePower4) {
        this.beginRatePower4 = beginRatePower4;
    }

    public BigDecimal getEndPower() {
        return endPower;
    }

    public void setEndPower(BigDecimal endPower) {
        this.endPower = endPower;
    }

    public BigDecimal getEndRatePower1() {
        return endRatePower1;
    }

    public void setEndRatePower1(BigDecimal endRatePower1) {
        this.endRatePower1 = endRatePower1;
    }

    public BigDecimal getEndRatePower2() {
        return endRatePower2;
    }

    public void setEndRatePower2(BigDecimal endRatePower2) {
        this.endRatePower2 = endRatePower2;
    }

    public BigDecimal getEndRatePower3() {
        return endRatePower3;
    }

    public void setEndRatePower3(BigDecimal endRatePower3) {
        this.endRatePower3 = endRatePower3;
    }

    public BigDecimal getEndRatePower4() {
        return endRatePower4;
    }

    public void setEndRatePower4(BigDecimal endRatePower4) {
        this.endRatePower4 = endRatePower4;
    }

    public BigDecimal getRatePrice1() {
        return ratePrice1;
    }

    public void setRatePrice1(BigDecimal ratePrice1) {
        this.ratePrice1 = ratePrice1;
    }

    public BigDecimal getRatePrice2() {
        return ratePrice2;
    }

    public void setRatePrice2(BigDecimal ratePrice2) {
        this.ratePrice2 = ratePrice2;
    }

    public BigDecimal getRatePrice3() {
        return ratePrice3;
    }

    public void setRatePrice3(BigDecimal ratePrice3) {
        this.ratePrice3 = ratePrice3;
    }

    public BigDecimal getRatePrice4() {
        return ratePrice4;
    }

    public void setRatePrice4(BigDecimal ratePrice4) {
        this.ratePrice4 = ratePrice4;
    }

    public BigDecimal getParkPrice() {
        return parkPrice;
    }

    public void setParkPrice(BigDecimal parkPrice) {
        this.parkPrice = parkPrice;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public BigDecimal getParkMoney() {
        return parkMoney;
    }

    public void setParkMoney(BigDecimal parkMoney) {
        this.parkMoney = parkMoney;
    }

    public BigDecimal getProcessPrice() {
        return processPrice;
    }

    public void setProcessPrice(BigDecimal processPrice) {
        this.processPrice = processPrice;
    }

    public BigDecimal getProcessFee() {
        return processFee;
    }

    public void setProcessFee(BigDecimal processFee) {
        this.processFee = processFee;
    }

    public BigDecimal getBeforeBalance() {
        return beforeBalance;
    }

    public void setBeforeBalance(BigDecimal beforeBalance) {
        this.beforeBalance = beforeBalance;
    }

    public BigDecimal getAfterBalance() {
        return afterBalance;
    }

    public void setAfterBalance(BigDecimal afterBalance) {
        this.afterBalance = afterBalance;
    }

    public String getCounter() {
        return counter;
    }

    public void setCounter(String counter) {
        this.counter = counter;
    }

    public String getRtua() {
        return rtua;
    }

    public void setRtua(String rtua) {
        this.rtua = rtua;
    }

    public String getCardVersion() {
        return cardVersion;
    }

    public void setCardVersion(String cardVersion) {
        this.cardVersion = cardVersion;
    }

    public String getPosNum() {
        return posNum;
    }

    public void setPosNum(String posNum) {
        this.posNum = posNum;
    }

    public String getCardStatus() {
        return cardStatus;
    }

    public void setCardStatus(String cardStatus) {
        this.cardStatus = cardStatus;
    }

    public String getSvin() {
        return svin;
    }

    public void setSvin(String svin) {
        this.svin = svin;
    }

    public String getZvin() {
        return zvin;
    }

    public void setZvin(String zvin) {
        this.zvin = zvin;
    }

    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public BigDecimal getServicePrice() {
        return servicePrice;
    }

    public void setServicePrice(BigDecimal servicePrice) {
        this.servicePrice = servicePrice;
    }

    public BigDecimal getPower() {
        return this.endPower.subtract(this.beginPower);
    }

    public BigDecimal getBalance() {
        return this.beforeBalance.subtract(this.afterBalance);
    }

    @Override
    public List<ElecRecordRequest> getResult(String dataStr) {
        List<ElecRecordRequest> list = new ArrayList<>();
        String begin = dataStr.substring(0, 5); // 开始点位置
        // （从1开始，后面是0:表示主动上传，1表示最近数据
        TYPE = begin.substring(0, 2);
        dataStr = dataStr.substring(6);
        String num = dataStr.substring(0, 2); // 记录条数
        dataStr = dataStr.substring(3);
        String record = null;
        while (dataStr.length() >= this.length) {
            record = dataStr.substring(0, this.length);
            ElecRecordRequest err = new ElecRecordRequest(record, this.source);
            if (SocketConstant.SOURCE_SHENZHEN.equalsIgnoreCase(this.source)) {// 深圳的，开始点位置就是测量点号/枪号
                err.setGunNum(begin.substring(0, 2));
            }
            list.add(err);
            if (dataStr.length() > this.length) {
                dataStr = dataStr.substring(this.length + 1);
            } else {
                break;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        // 深圳桩数据
        String data = "01 00 01 01 66 01 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 00 20 69 01 00 80 30 00 00 00 86 00 00 00 32 00 00 00 18 00 00 99 69 01 00 59 31 00 00 00 86 00 00 00 32 00 00 00 18 00 00 00 30 04 00 50 06 00 20 03 00 50 04 00 00 00 29 44 13 15 03 17 00 47 13 15 03 17 00 00 00 00 00 00 00 00 00 00 77 93 64 09 34 90 64 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30";
        // 珠海桩数据
        String data1 = "00 00 01 00 91 52 50 89 14 00 00 00 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 00 00 01 00 00 01 00 00 00 11 28 15 14 03 17 11 28 15 14 03 17 00 00 00 66 98 00 00 66 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00";
        String data2 = "00 00 01 00 91 52 50 89 14 00 00 00 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 00 00 01 00 00 01 00 00 00 11 28 15 14 03 17 11 28 15 14 03 17 00 00 00 66 98 00 00 66 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00";
        String data3 = "00 00 01 00 08 41 37 90 14 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 01 00 01 00 00 54 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 54 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 60 00 00 60 00 00 00 01 00 00 01 00 00 00 28 48 16 24 03 17 28 48 16 24 03 17 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00";
        String data4 = "00 00 01 00 21 57 61 90 14 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 01 00 01 00 10 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 00 00 01 00 00 01 00 00 00 21 55 11 27 03 17 21 55 11 27 03 17 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00";
        String response = "68 08 20 01 00 02 00 68 03 06 00 21 57 61 90 14 81 16";
        String response1 = "68 82 05 14 00 02 00 68 03 06 00 00 91 52 50 89 14 46 16";
        String shenZhen = "02 00 01 01 40 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 60 41 02 00 20 85 00 00 20 27 01 00 00 10 00 00 40 18 00 00 39 42 02 00 99 85 00 00 20 27 01 00 00 10 00 00 40 18 00 00 00 21 01 00 98 00 00 00 00 00 76 00 00 00 00 23 56 18 31 03 17 10 00 19 31 03 17 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30";
        String str = "68 19 00 71 01 00 00 68 83 9B 00 01 00 01 01 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 02 00 00 00 00 18 17 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 25 53 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 53 01 00 53 01 00 53 01 00 53 01 00 00 00 00 55 42 09 11 05 17 03 00 11 11 05 17 00 00 00 00 00 00 00 00 00 00 47 85 00 00 47 85 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 DA 16";
        String ex = "68 19 00 01 08 00 00 68 83 9B 00 01 00 01 00 13 02 00 00 00 00 00 00 00 00 00 00 00 00 00 35 00 00 00 00 00 00 00 35 00 00 00 00 45 36 21 00 00 00 00 00 00 00 00 00 90 66 15 00 24 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 90 66 15 00 24 00 05 00 60 01 00 60 01 00 60 01 00 20 01 00 00 00 00 55 53 11 11 05 17 55 53 11 11 05 17 00 00 00 00 00 00 00 00 00 00 07 56 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 16";
        String d = "68 19 00 61 01 00 00 68 83 9B 00 01 00 01 01 07 04 00 00 00 00 00 00 20 17 04 26 23 09 17 00 20 17 04 26 23 09 17 00 00 00 00 00 60 15 77 00 40 13 00 00 00 25 18 00 40 65 07 00 59 17 04 00 80 15 77 00 40 13 00 00 00 25 18 00 59 65 07 00 59 17 04 00 53 01 00 53 01 00 53 01 00 53 01 00 00 00 00 08 46 16 07 06 17 42 46 16 07 06 17 00 00 00 00 00 00 00 00 00 00 38 70 07 00 08 70 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 28 16";
        String a = "68 19 00 01 08 00 00 68 83 9B 00 02 00 01 01 58 00 00 00 00 00 00 00 20 17 06 09 10 56 09 00 20 17 06 09 10 56 09 00 00 00 00 00 75 50 31 00 35 17 02 00 10 53 00 00 90 87 18 00 25 17 09 00 75 50 31 00 35 17 02 00 10 53 00 00 90 87 18 00 25 17 09 00 60 01 00 20 01 00 60 01 00 98 00 00 00 00 00 40 40 11 09 06 17 20 41 11 09 06 17 00 00 00 00 00 00 00 00 00 00 51 07 00 00 51 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 B6 16";
        String b = "68 19 00 61 12 02 00 68 81 2C 00 02 00 00 00 00 00 00 00 31 C8 FF FF FF FF FF FF FF FF 88 C8 79 38 33 89 C8 89 04 00 04 C9 EC 13 8D C8 49 07 00 00 8E C8 90 08 00 00 6F 16 68 19 00 61 12 00 00 68 83 9B 00 02 00 01 01 26 00 00 00 00 00 00 00 20 17 04 14 07 37 31 00 20 17 04 14 07 37 31 00 00 00 00 00 10 65 00 00 60 15 00 00 00 00 00 00 55 23 00 00 60 24 00 00 84 65 00 00 60 15 00 00 00 00 00 00 29 24 00 00 60 24 00 00 20 01 00 20 01 00 20 01 00 20 01 00 00 00 00 58 33 15 12 06 17 30 37 15 12 06 17 00 00 00 00 00 00 00 00 00 00 24 71 08 00 35 70 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 AD 16 68 19 00 61 12 02 00 68 A4 00 00 02 16 68 19 00 61 12 00 00 68 83 9B 00 02 00 01 01 26 00 00 00 00 00 00 00 20 17 04 14 07 37 31 00 20 17 04 14 07 37 31 00 00 00 00 00 10 65 00 00 60 15 00 00 00 00 00 00 55 23 00 00 60 24 00 00 84 65 00 00 60 15 00 00 00 00 00 00 29 24 00 00 60 24 00 00 20 01 00 20 01 00 20 01 00 20 01 00 00 00 00 58 33 15 12 06 17 30 37 15 12 06 17 00 00 00 00 00 00 00 00 00 00 24 71 08 00 35 70 08 00 00 00";
        String c = "68 19 00 81 12 00 00 68 83 9B 00 01 00 01 01 35 00 00 00 00 00 00 00 20 17 04 13 15 42 53 00 20 17 04 13 15 42 53 00 00 00 00 00 55 26 00 00 15 03 00 00 20 10 00 00 00 12 00 00 00 00 00 00 90 27 00 00 50 04 00 00 20 10 00 00 00 12 00 00 00 00 00 00 20 01 00 20 01 00 20 01 00 20 01 00 00 00 00 25 24 18 12 06 17 15 30 18 12 06 17 00 00 00 00 00 00 00 00 00 00 67 66 89 00 05 65 89 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 FD 16";
        String e = "68 19 00 01 08 00 00 68 83 9B 00 02 00 01 01 31 00 00 00 00 00 00 00 20 17 06 07 14 28 12 00 20 17 06 07 14 28 12 00 00 00 00 00 95 02 35 00 65 65 02 00 80 03 01 00 75 71 19 00 60 86 10 00 49 38 35 00 65 65 02 00 80 03 01 00 75 71 19 00 14 22 11 00 60 01 00 20 01 00 60 01 00 98 00 00 00 00 00 18 14 01 14 06 17 09 25 02 14 06 17 00 00 00 00 00 00 00 00 00 00 91 36 00 00 08 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 01 16";
        List<TerminalMessage> set = MessageUtil.decoder(e);
        for (TerminalMessage msg : set) {
            //System.out.println(msg.getContral());
            if (msg.getContral().equals("83")) {
                System.out.println(msg.getData());
                Result result = new ElecRecordRequest(msg.getData().split(" ").length - 3);
                List<ElecRecordRequest> erList = result.getResult(msg.getData());
                for (ElecRecordRequest err : erList) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //20 17 04 14 07 37 31 00
                    System.out.println(err.getSerialNum());
                    System.out.println(err.getBeginCard());
                    System.out.println(err.getEndCard());
                    System.out.println(err.getBeforeBalance());
                    System.out.println(err.getAfterBalance());
                    System.out.println(sdf.format(err.getBeginTime()));
                    System.out.println(sdf.format(err.getEndTime()));
                    System.out.println(err.getBeforeBalance().subtract(err.getAfterBalance()));
                    System.out.println(err.getBeginPower());
                    System.out.println(err.getEndPower());
                    System.out.println(err.getTradeType());
                    System.err.println(err.getGunNum());
                }
            }
        }

    }
}
